package com.yf.aclient.threads;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import android.os.Handler;
import android.os.Message;

public class UploadThread extends Thread {
	
	private String file;
	private  String type;
	private  String serverUrl;
	private  Handler handler ;
	private  String  memo;
//	private  Handler
	public UploadThread(String file,String memo,String type,String serverUrl,Handler handler){
		this.file=file;
		this.type=type;
		this.serverUrl=serverUrl;
		this.handler=handler;
		this.memo=memo;
	}
	
	public void  run(){
		uploadFile(file,type,serverUrl);
	}
	private void uploadFile(String uploadFile,String type,String serverUrl) {
		String nid=URLEncoder.encode(uploadFile);
		
		String contentType="image";
		if(type.equals("1")){
			contentType="image";
		}else if(type.equals("2")){
			contentType="text";
		}else if(type.equals("3")){
			contentType="audio";
		}else{
			contentType="video";
		}
		
		
		String  uploadActionUrl=serverUrl+"/UploadSevelet";
		String end = "\r\n";
		String twoHyphens = "--";
		String boundary = "*****";
		int index = uploadFile.lastIndexOf("\\");
		String filename = uploadFile.substring(index + 1, uploadFile.length());
		String fileType=filename.substring(filename.lastIndexOf("."),filename.length());
		try {
			URL url = new URL(uploadActionUrl);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			/* 允许Input、Output，不使用Cache */
			con.setDoInput(true);
			con.setDoOutput(true);
			con.setUseCaches(false);
			 con.setConnectTimeout(6000);// 6秒钟连接超时
			con.setReadTimeout(250000);// 250秒钟读数据超时
			// con.set
			/* 设置传送的method=POST */
			con.setRequestMethod("POST");
			/* setRequestProperty */
			con.setRequestProperty("Connection", "Keep-Alive");
			con.setRequestProperty("Charset", "UTF-8");
			con.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);
			/* 设置DataOutputStream */
			DataOutputStream ds = new DataOutputStream(con.getOutputStream());
			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; "
					+ "name=\"type\"");
			ds.writeBytes(end);
			ds.writeBytes(end);
			ds.writeBytes(type);
			ds.writeBytes(end);
			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; "
					+ "name=\"nid\"");
			ds.writeBytes(end);
			ds.writeBytes(end);
			ds.writeBytes(nid);
			ds.writeBytes(end);
			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; "
					+ "name=\"memo\"");
			ds.writeBytes(end);
			ds.writeBytes(end);
			ds.writeBytes(URLEncoder.encode(memo));
			ds.writeBytes(end);
			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; "
					+ "name=\"file1\";filename=\"");
			ds.write(filename.getBytes("UTF-8"));
			ds.writeBytes("\"" + end);
			ds.writeBytes("Content-Type: "+contentType+"/"+fileType + end);
			ds.writeBytes(end);
			/* 取得文件的FileInputStream */
			FileInputStream fStream = new FileInputStream(uploadFile);
			/* 设置每次写入1024bytes */
			int bufferSize = 1024;
			byte[] buffer = new byte[bufferSize];
			int length = -1;
			/* 从文件读取数据至缓冲区 */
			while ((length = fStream.read(buffer)) != -1) {
				/* 将资料写入DataOutputStream中 */
				ds.write(buffer, 0, length);
			}
			ds.writeBytes(end);
			ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
			/* close streams */
			fStream.close();
			ds.flush();
			/* 取得Response内容 */
			InputStream is = null;
			try {
				is = con.getInputStream();
			} catch (Exception e) {
				e.printStackTrace();
			}
			InputStreamReader isr = new InputStreamReader(is, "utf-8");
			BufferedReader br = new BufferedReader(isr);
			String info = br.readLine();
			Message msg=Message.obtain();
			msg.obj=info;
			handler.sendMessage(msg);
			con.disconnect();
			is.close();
			ds.close();
		} catch (Exception e) {
			Message msg=Message.obtain();
			msg.obj="上传失败"+e.getMessage();
			handler.sendMessage(msg);
			e.getStackTrace();
			// showDialog("上传失败" + e);
		}
	}
}
